package com.example.demo_homework2.controller;

import com.example.demo_homework2.bean.CmsComment;
import com.example.demo_homework2.common.Response;
import com.example.demo_homework2.mapper.CmsCommentMapper;
import com.example.demo_homework2.service.CmsCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "评论信息管理")
@RequestMapping("/comment")
public class CmsCommentController {
    @Autowired
    CmsCommentService commentService;

    @PostMapping("/saveorupdate")
    @ApiOperation(value = "保存或更新评论信息", notes = "如果传参时指明了评论编号，执行更新操作<br/>"
            + "如果传参时没有指明评论编号，执行插入操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评论编号，更新时必须传入"),
            @ApiImplicitParam(name = "content", value = "评论内容"),
            @ApiImplicitParam(name = "artId", value = "文章"),
            @ApiImplicitParam(name = "memberName", value = "评论用户")
    })
    public Response<String> saveOrUpdate(String id, String content, String artId, String memberName) {
        boolean b = commentService.saveOrUpdate(id, content, artId, memberName);
        if (b) {
            return Response.ok("操作成功");
        } else {
            return Response.error("操作失败");
        }
    }

    @DeleteMapping("/deletebyid")
    @ApiOperation(value = "按评论编号删除评论")
    @ApiImplicitParam(name = "id", value = "要删除的栏目编号")
    public Response<String> deleteByid(String id) {
        int i = commentService.delete(id);
        if (i!=0) {
            return Response.ok("删除成功");
        } else {
            return Response.error("删除失败");
        }
    }
    @GetMapping("/all")
    @ApiOperation(value = "显示所有评论信息")
    public Response<List<CmsComment>> seleteAll(){
        return Response.ok(commentService.all());
    }
    @DeleteMapping("/deleteByIds")
    @ApiOperation(value ="按评论编号批量删除评论")
    public Response<String> deleteByIds(String[] ids) {
        String s = commentService.deleteByIds(ids);
        if (s.startsWith("success")) {
            return Response.ok(s);
        } else {
            return Response.error(s);
        }
    }
    @PutMapping("/updateState")
    @ApiOperation(value = "审核评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "comId", value = "评论编号"),
            @ApiImplicitParam(name = "state", value = "状态", allowableValues = "已通过,未通过")
    })
    public Response<String> updateState(String comId, String state) {
        boolean b = commentService.updateState(comId, state);
        if (b) {
            return Response.ok("操作成功");
        } else {
            return Response.error("要更新状态的不存在");
        }
    }
}
